package cn.com.dhcc.app.pub.core.listener;

import javax.servlet.ServletContextEvent;

import cn.com.dhcc.app.core.AppEnv.ParaGetTool;
import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.handler.remotehandler.rmi.RmiServerCenter;
import cn.com.dhcc.app.core.handler.remotehandler.socket.ISocketServer;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.KernelParam;
import cn.com.dhcc.turbo.service.registry.sync.SynchronizeHandler;
import cn.com.dhcc.turbo.service.rmi.RmiRemoteServiceImpl;

/**
 * 启动同步用的RMI服务
 */
public class RmiServerInitListener implements AppInitListener {

	ISocketServer iSocketServer = null;
	public static int  rmiServerPort = ParaGetTool.getPara(Integer.class, "app.rmi.port", 9160);
	
	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam) throws InitException {
		try {
			RmiServerCenter.startRmiServer(AppConst.installIp,rmiServerPort);
			contextEvent.getServletContext().log("同步服务监听启动成功,ip:["+AppConst.installIp+"],port["+rmiServerPort+"]");
			RmiServerCenter.bindService(AppConst.installIp,rmiServerPort,RMISERICE_NAMESPACE.SYNCSERVER.name(), CM.getBean(SynchronizeHandler.class));
			
			
			//
			if (KernelParam.tlqTransportPipelineEnable==0) {//如果没有启用tlq，则启用rmi
				RmiServerCenter.bindService(AppConst.installIp,rmiServerPort,RMISERICE_NAMESPACE.RMIMESSAGE.name(), CM.getBean(RmiRemoteServiceImpl.class));
				
			}
			
			contextEvent.getServletContext().log("注册同步服务成功 ,服务名称 ["+RMISERICE_NAMESPACE.SYNCSERVER.desc+"]");
		} catch (NiceException e) {
			contextEvent.getServletContext().log(e.getMessage(),e);
		}
	}
	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam) throws Exception {
		try {
			RmiServerCenter.clostRmiServer();
		} catch (Exception e) {
			throw e;
		}
	}
	public static enum RMISERICE_NAMESPACE{
		SYNCSERVER("同步数据接收服务"),
		RMIMESSAGE("消息消费服务");
		private String desc;
		RMISERICE_NAMESPACE(String desc){
			this.desc = desc;
		}
		public String getDesc() {
			return desc;
		}
		public void setDesc(String desc) {
			this.desc = desc;
		}
		
	}
}
